{
 "metadata": {
  "signature": "sha256:eb79120b33a7bf15424823de863e13dcb75b0fc709e1152a578780ede763448b"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Matplotlib: using matplotlib in a CGI script\n",
      "======================================================================\n",
      "\n",
      "Trying to use matplotlib in a python CGI script na\u00efvely will most likely\n",
      "result in the following error:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "...\n",
      "352, in _get_configdir\n",
      "raise RuntimeError(\"'%s' is not a writable dir; you must set\n",
      "environment variable HOME to be a writable dir \"%h)\n",
      "RuntimeError: '<WebServer DocumentRoot>' is not a writable dir; you must set\n",
      "environment variable HOME to be a writable dir"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Matplotlib needs the environment variable HOME to point to a writable\n",
      "directory. One way to accomplish this is to set this environment\n",
      "variable from within the CGI script on runtime (another way would be to\n",
      "modify the file but that would be not as portable). The following\n",
      "template can be used for a cgi that uses matplotlib to create a png\n",
      "image:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#!/usr/bin/python\n",
      "import os,sys\n",
      "import cgi\n",
      "import cgitb; cgitb.enable()\n",
      "\n",
      "# set HOME environment variable to a directory the httpd server can write to\n",
      "os.environ[ 'HOME' ] = '/tmp/'\n",
      "\n",
      "import matplotlib\n",
      "# chose a non-GUI backend\n",
      "matplotlib.use( 'Agg' )\n",
      "\n",
      "import pylab\n",
      "\n",
      "#Deals with inputing data into python from the html form\n",
      "form = cgi.FieldStorage()\n",
      "\n",
      "# construct your plot\n",
      "pylab.plot([1,2,3])\n",
      "\n",
      "print \"Content-Type: image/png\\n\"\n",
      "\n",
      "# save the plot as a png and output directly to webserver\n",
      "pylab.savefig( sys.stdout, format='png' )"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "This image can then be accessed with a URL such as:\n",
      "<http://localhost/showpng.py>\n",
      "\n",
      "As documented,some backends will not allow the output to be sent to\n",
      "sys.stdout. It is possible to replace the last line with the following\n",
      "to work around this:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "pylab.savefig( \"tempfile.png\", format='png' )\n",
      "import shutil\n",
      "shutil.copyfileobj(open(\"tempfile.png\",'rb'), sys.stdout)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "(Of course it is necessary to create and delete proper temp files to use this in production.)"
     ]
    }
   ],
   "metadata": {}
  }
 ]
}